// pages/bindingGuild/President/binding/binding.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: { // 选择平台后返回
    p_id: null, // 平台id
    p_name: null, //	平台名称
    p_is_paltfrom: null, // 是否全平台
    p_table_flag: null, //标签

    checkes: false,
    disabled: true,

    text_guild: null,
    text_guildId: null,
    text_anchorId: null,

    reg: false,

  },
  //正则判定
  validateInput: function(text) {
    var data = text;
    var pattern = /^[\d;]+$/;
    if (!pattern.test(data)) {
      wx.showToast({
        icon: "none",
        title: '只能输入数字和英文分号!',
      })
      this.setData({
        reg: false,
      })
      // console.log("只能输入数字或者英文分号!");
    } else {
      this.setData({
        reg: true,
      })
    }
  },
  go_protocol: function() {
    wx.navigateTo({
      url: '../../../protocol/protocol',
    })
  },
  go_selec: function() {
    wx.redirectTo({
      url: '../../platform/platform?process_flag=president_bind',
    })
  },
  checkboxChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      checkes: e.detail.value,
    })
  },
  //点击事件
  click_guild: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  click_guildId: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  click_anchorId: function() {
    if (this.data.p_id) {
      this.setData({
        disabled: false
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '请先选择直播平台',
      })
    }
  },
  //输入事件
  inp_guild: function(e) {
    // console.log(e.detail.value)
    this.setData({
      text_guild: e.detail.value,
    })
  },
  inp_guildId: function(e) {
    // console.log(e.detail.value)
    this.setData({
      text_guildId: e.detail.value,
    })
  },
  inp_anchorId: function(e) {
    // console.log(e.detail.value)
    this.validateInput(e.detail.value)
    this.setData({
      text_anchorId: e.detail.value,
    })
  },
  //立即绑定
  click_bind: function() {
    var that = this;
    if (this.data.checkes == "check") {
      // console.log("ok")
      if (this.data.text_guild && (this.data.text_guildId || this.data.text_anchorId)) {
        // console.log("ok two")
        if (this.data.text_guildId) {
          //走公会id
          wx.request({
            method: "POST",
            url: app.globalData.url + 'api/mine/guild/quick/binding',
            data: {
              token: app.globalData.token, // token
              plat_id: this.data.p_id, //	平台id
              plat_name: this.data.p_name, // 平台名称
              guild_name: this.data.text_guild, // 公会名称
              q_guild_id: this.data.text_guildId, // 全网平台id
              anchor_id: this.data.text_anchorId, // 主播id
              table_flag: this.data.p_table_flag, // 标签
            },
            success: function(e) {
              console.log(e)

              if (e.data.code == "-1") {
                wx.showToast({
                  icon: "none",
                  title: e.data.message,
                })
              } else if (e.data.code == "0") {
                that.setData({
                  guild_id: e.data.data.guild_id,
                })
                wx.showModal({
                  content: '小主~恭喜你绑定成功啦~',
                  cancelText: "我先体验",
                  cancelColor: "#ddba69",
                  confirmText: "升级公会",
                  confirmColor: "#ddba69",
                  success: function(e) {
                    console.log(e.cancel)
                    if (e.cancel) {
                      console.log("取消1")
                      wx.navigateTo({
                        url: '../../../report/report',
                      })
                    } else {
                      console.log("确定1")
                      wx.navigateTo({
                        url: '../../../bindingGuild/President/authenticate/authenticate?guild_id=' + that.data.guild_id + '&&guild_name=' + that.data.text_guild + '&&type=upgrade',
                      })
                    }

                  },
                })
              }
            }
          })
        } else if (this.data.text_anchorId) {
          //走主播id
          if (this.data.reg) {
            //正则验证正确
            wx.request({
              method: "POST",
              url: app.globalData.url + 'api/mine/guild/quick/binding',
              data: {
                token: app.globalData.token, // token
                plat_id: this.data.p_id, //	平台id
                plat_name: this.data.p_name, // 平台名称
                guild_name: this.data.text_guild, // 公会名称
                anchor_id: this.data.text_anchorId, // 主播id
                table_flag: this.data.p_table_flag, // 标签
              },
              success: function (e) {
                console.log(e)

                if (e.data.code == "-1") {
                  wx.showToast({
                    icon: "none",
                    title: e.data.message,
                  })
                } else if (e.data.code == "0") {
                  that.setData({
                    guild_id: e.data.data.guild_id,
                  })
                  wx.showModal({
                    content: '小主~恭喜你绑定成功啦~',
                    cancelText: "我先体验",
                    cancelColor: "#ddba69",
                    confirmText: "升级公会",
                    confirmColor: "#ddba69",
                    success: function (e) {
                      console.log(e.cancel)
                      if (e.cancel) {
                        console.log("取消2")
                        wx.navigateTo({
                          url: '../../../report/report',
                        })
                      } else {
                        console.log("确定2")
                        wx.navigateTo({
                          url: '../../../bindingGuild/President/authenticate/authenticate?guild_id=' + that.data.guild_id + '&&guild_name=' + that.data.text_guild + '&&type=upgrade',
                        })
                      }

                    },
                  })
                } else { }
              }
            })
          } else {
            //正则验证失败
            wx.showToast({
              icon: "none",
              title: '请确认主播id格式正确',
            })
          }

        } else {}
      } else {
        wx.showToast({
          icon: "none",
          title: '请先填写完整信息',
        })
      }
    } else {
      wx.showToast({
        icon: "none",
        title: '请先阅读并勾选服务协议与保密协议',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title //页面标题为路由参数
      })
    }
    if (options.p_id) {
      let p_id = (options.p_id).toString();
      let p_name = options.p_name;
      let p_is_paltfrom = options.p_is_paltfrom;
      let p_table_flag = options.p_table_flag;
      this.setData({
        p_id: p_id, //选择平台后传回的平台id
        p_name: p_name,
        p_is_paltfrom: p_is_paltfrom,
        p_table_flag: p_table_flag,
      })
    }
    // console.log(this.data.p_name)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})